package com.pet.customer.controller;

import com.pet.customer.mapper.*;
import com.pet.customer.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
public class IndexControl {
    @Autowired
    private BannerMapper bannerMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private EnrollMapper enrollMapper;

    @RequestMapping(value="/index",method = RequestMethod.GET)
    public String index(Model model) {
        List<Banner> bannerList = bannerMapper.selectByExample(new BannerExample());
        if(bannerList.get(0)!=null){
            model.addAttribute("banner1",bannerList.get(0).getName());
        }else{
            model.addAttribute("banner1","b1.png");
        }
        if(bannerList.get(1)!=null){
            model.addAttribute("banner2",bannerList.get(1).getName());
        } else{
            model.addAttribute("banner2","b2.png");
        }
        if(bannerList.get(2)!=null){
            model.addAttribute("banner3",bannerList.get(2).getName());
        }else{
            model.addAttribute("banner3","b3.png");
        }

        List<Shop> shopList = shopMapper.selectByExample(new ShopExample());
        model.addAttribute("shops",shopList);
        return "home";
    }

    @RequestMapping(value="/activities",method = RequestMethod.GET)
    public String activities(Model model) {

        List<Activity> activities = activityMapper.selectByExample(new ActivityExample());
        model.addAttribute("activities",activities);
        return "activities";
    }

    @RequestMapping(value="/shop",method = RequestMethod.GET)
    public String shop(HttpServletRequest request,Model model) {
        String id = request.getParameter("id");

        ShopExample shopExample = new ShopExample();
        shopExample.createCriteria().andIdEqualTo(Integer.parseInt(id));
        List<Shop> shops = shopMapper.selectByExample(shopExample);
        Shop shop = new Shop();
        if(shops.size()>0) {
            shop = shopMapper.selectByExample(shopExample).get(0);
        }
        model.addAttribute("shop",shop);

        GoodsExample goodsExample  = new GoodsExample();
        goodsExample.createCriteria().andEmailEqualTo(shop.getEmail());
        List<Goods> goods =  goodsMapper.selectByExample(goodsExample);
        model.addAttribute("goods",goods);
        return "shop";
    }

    @RequestMapping(value="/login",method = RequestMethod.GET)
    public String login(Model model) {

        return "index";
    }

    @RequestMapping(value="/register",method = RequestMethod.GET)
    public String register(Model model) {

        return "register";
    }

    @RequestMapping(value="/logout",method = RequestMethod.GET)
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("email");
        return "index";
    }

    @RequestMapping(value="/item",method = RequestMethod.GET)
    public String item(HttpServletRequest request,Model model) {
        String id = request.getParameter("id");
        String shopId = request.getParameter("shopId");

        Goods good = goodsMapper.selectByPrimaryKey(Integer.parseInt(id));
        model.addAttribute("good",good);

        Shop shop = shopMapper.selectByPrimaryKey(Integer.parseInt(shopId));
        model.addAttribute("shop",shop);
        return "item";
    }

    @RequestMapping(value="/api/buyone",method = RequestMethod.GET)
    public void buyone(HttpServletRequest request, HttpServletResponse response,Model model) throws IOException {
        String email = (String)request.getSession().getAttribute("email");
        String goodsId = request.getParameter("goodsId");
        Goods goods = goodsMapper.selectByPrimaryKey(Integer.parseInt(goodsId));
        String shopId = request.getParameter("shopId");
        Shop shop = shopMapper.selectByPrimaryKey(Integer.parseInt(shopId));
        Orders orders = new Orders();
        orders.setOrdertime(new Date());
        orders.setCustomeremail(email);
        orders.setShopid(Integer.parseInt(shopId));
        orders.setGoodsid(Integer.parseInt(goodsId));
        orders.setStatus("ORDERED");
        orders.setGoodsname(goods.getName());
        orders.setShopname(shop.getName());
        orders.setPrice(goods.getPrice());

        ordersMapper.insertSelective(orders);

        response.sendRedirect(request.getContextPath()+"/api/orders");
    }

    @RequestMapping(value="/api/orders",method = RequestMethod.GET)
    public String orders(HttpServletRequest request,Model model) {
        String email = (String) request.getSession().getAttribute("email");
        OrdersExample ordersExample = new OrdersExample();
        ordersExample.createCriteria().andCustomeremailEqualTo(email);
        List<Orders> ordersList = ordersMapper.selectByExample(ordersExample);
        model.addAttribute("orders",ordersList);

        return "orders";
    }

    @RequestMapping(value="/api/enroll",method = RequestMethod.POST)
    public void enroll(HttpServletRequest request, HttpServletResponse response,Model model) throws IOException {
        String email = (String)request.getSession().getAttribute("email");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String actId = request.getParameter("actId");

        EnrollExample enrollExample = new EnrollExample();
        enrollExample.createCriteria().andCustomeremailEqualTo(email).andActivityidEqualTo(Integer.parseInt(actId));
        enrollMapper.deleteByExample(enrollExample);

        Enroll enroll = new Enroll();
        enroll.setActivityid(Integer.parseInt(actId));
        enroll.setAddress(address);
        enroll.setCustomeremail(email);
        enroll.setName(name);
        enroll.setPhone(phone);
        enrollMapper.insertSelective(enroll);
        response.sendRedirect(request.getContextPath()+"/activities");
    }
}